package com.sjd.mall.service.goods.impl;


import com.sjd.common.core.domain.AjaxResult;
import com.sjd.mall.domain.SjdGoodsTypeDO;
import com.sjd.mall.domain.dto.goods.*;
import com.sjd.mall.mapper.SjdGoodsTypeMapper;
import com.sjd.mall.service.goods.GoodsTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 产品类型实现类
 * @date : 2022-01-20 17:30
 */
@Slf4j
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    public static Map<String, Integer> NAME_TYPES = new HashMap<>(16);

    static {
        NAME_TYPES.put("goods", 1);
        NAME_TYPES.put("lessons", 2);
    }

    @Autowired
    private SjdGoodsTypeMapper sjdGoodsTypeMapper;

    /**
     * 添加
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public AjaxResult addGoodType(SjdGoodsTypeAddRequestDTO requestDTO) {
        SjdGoodsTypeDO sjdGoodsTypeDO = SjdGoodsTypeDO.builder()
                .name(requestDTO.getName())
                .typeClass(requestDTO.getTypeClass())
                .picture(requestDTO.getPictureUrl())
                .creatorId(requestDTO.getCreatorId())
                .parentId(requestDTO.getParentId() == null ? 0 : requestDTO.getParentId())
                .build();
        sjdGoodsTypeMapper.insertSelective(sjdGoodsTypeDO);
        return AjaxResult.success();
    }

    /**
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public AjaxResult updateGoodType(SjdGoodsTypeUpdateRequestDTO requestDTO) {
        SjdGoodsTypeDO sjdGoodsTypeDO = SjdGoodsTypeDO.builder()
                .goodsTypeId(requestDTO.getGoodsTypeId())
                .name(requestDTO.getName())
                .picture(requestDTO.getPictureUrl())
                .typeClass(requestDTO.getTypeClass())
                .parentId(requestDTO.getParentId() == null ? 0 : requestDTO.getParentId())
                .build();
        sjdGoodsTypeMapper.updateByPrimaryKeySelective(sjdGoodsTypeDO);
        return AjaxResult.success();
    }

    /**
     * 获取所有未删除的商品类型 管理平台
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public List<SjdWebSiteGoodsTypeQueryResponseDTO> queryTypesWebSite(SjdWebSiteGoodsTypeQueryRequestDTO requestDTO) {
        List<SjdWebSiteGoodsTypeQueryResponseDTO> types = sjdGoodsTypeMapper.getWebSiteGoodsTypesByTypeClass(requestDTO.getTypeClass());
        if (CollectionUtils.isEmpty(types)) {
            return new ArrayList<>();
        }
        return types;
    }

    /**
     * 查询商品分类 小程序
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public List<SjdAppGoodsTypeQueryResponseDTO> queryTypesApp(SjdAppGoodsTypeQueryRequestDTO requestDTO) {
        List<SjdGoodsTypeDO> goodsTypesByTypeClass = sjdGoodsTypeMapper.getAppGoodsTypesByTypeClass(requestDTO.getTypeClass());
        List<SjdAppGoodsTypeQueryResponseDTO> responseDTOList = new ArrayList<>(32);
        if (CollectionUtils.isEmpty(goodsTypesByTypeClass)) {
            return responseDTOList;
        }
        for (SjdGoodsTypeDO sjdGoodsTypeDO : goodsTypesByTypeClass) {
            SjdAppGoodsTypeQueryResponseDTO responseDTO = SjdAppGoodsTypeQueryResponseDTO.builder()
                    .goodsTypeId(sjdGoodsTypeDO.getGoodsTypeId())
                    .name(sjdGoodsTypeDO.getName())
                    .picture(sjdGoodsTypeDO.getPicture())
                    .typeClass(sjdGoodsTypeDO.getTypeClass())
                    .build();
            responseDTOList.add(responseDTO);
        }
        return responseDTOList;
    }

    /**
     * 删除
     *
     * @param goodsTypeId goodsTypeId
     * @return return
     */
    @Override
    public AjaxResult delGoodsType(Long goodsTypeId) {
        sjdGoodsTypeMapper.deleteByPrimaryKey(goodsTypeId);
        return AjaxResult.success();
    }
}
